package redis;

import base.ApplicationTestBase;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.myBlog.pojo.Article;
import com.myBlog.pojo.Remark;
import com.myBlog.services.Article.ArticleService;
import com.myBlog.services.Remark.RemarkCaches;
import com.myBlog.services.Remark.RemarkService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;

@Slf4j
public class RedisTestBase  extends ApplicationTestBase {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    ArticleService articleService;

    @Autowired
    RemarkService remarkService;

    @Test
    void setRedis(){
        Page<Article> page = new Page(2, 3);
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.gt("articleId",5);
        Page<Article> page1 = articleService.page(page,wrapper);

        log.warn(page1.getRecords().toString());

//        redisTemplate.setValueSerializer( new StringRedisSerializer());
        redisTemplate.opsForList().leftPushAll("page_list",page1.getRecords());
//        redisTemplate.opsForValue().set("page1",page1.getRecords().toString());
    }

    @Test
    void getRedis(){
        // -1 表示最右边的元素，-2表示最右边第二个元素
        List list = redisTemplate.opsForList().range("page_list", 0, -1);
        log.warn(list.toString());
    }


    @Autowired
    RemarkCaches remarkCaches;

    @Test
    void testRemarkCaches(){
        List<Remark> remarks = remarkService.list( new QueryWrapper<Remark>().orderByDesc("remark_time") );
//        remarkCaches.setCache(remarks,);

        Page remark_cache = remarkCaches.getCache(1, 2);
        System.out.println(remark_cache);
    }
}
